package com.xh.controller.data;


import com.xh.service.LinkService;
import com.xh.vo.Link;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LinkController {
    @Resource
    private LinkService linkService;
    @GetMapping("Link")
    public String getLink(@RequestParam("name") String name, HttpSession session, Model model)  {
        List<Link> links = linkService.queryAllLink(Integer.parseInt(session.getAttribute("id")+""));
        model.addAttribute("links",links);
        String s = linkService.queryLink(name);
        session.setAttribute("link",s);
        return "blog/iframe";
    }

    @GetMapping("newLink")
    public String newLink()  {
        return "blog/collection";
    }
    @PostMapping("insertLink")
    public String insertLink(@RequestParam("name") String name,
                             @RequestParam("userId") Integer userId,
                             @RequestParam("link") String link){
        Link link1 = new Link();
        link1.setName(name);
        link1.setLink(link);
        link1.setUserId(userId);
        linkService.insertLink(link1);
        return "blog/collection";
    }

}
